package com.huluobo.a2110mvpimdemo.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2110mvpimdemo.R;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by LC on 2023/12/25.
 */
public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //发送消息的类型
    public static final int ITEM_TYPE_SEND_MESSAGE = 0;
    //接收消息的类型
    public static final int ITEM_TYPE_RECEIVE_MESSAGE = 1;

    //数据源,所有消息的集合
    private List<EMMessage> messages;

    public MessageAdapter(List<EMMessage> messages) {
        this.messages = messages;
    }

    //根据数据源中消息发送的方向,来给item赋值viewType
    //多布局的关键
    //判断消息方向,如果是发送消息就是0,接收消息就是1
    @Override
    public int getItemViewType(int position) {
        if (messages.get(position).direct() == EMMessage.Direct.SEND) {
            return ITEM_TYPE_SEND_MESSAGE;
        } else {
            return ITEM_TYPE_RECEIVE_MESSAGE;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_SEND_MESSAGE) {
            //如果viewType等于0,就是发送的布局
            return new SendMessageViewHolder(
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.view_send_message_item, parent, false)
            );
        } else {
            //如果viewType等于1,就是接收的布局
            return new ReceiveMessageViewHolder(
                    LayoutInflater.from(parent.getContext()).inflate(R.layout.view_receive_message_item, parent, false)
            );
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //如果item的类型等于发送的类型
        if (getItemViewType(position) == ITEM_TYPE_SEND_MESSAGE) {
            //通过多布局的type来确定使用哪个holder,通过holder才能拿到对应的view进行赋值
            SendMessageViewHolder sendMessageViewHolder = (SendMessageViewHolder) holder;
            //通过emMessage的body才能拿到具体的信息,需要强转
            String message = ((EMTextMessageBody) messages.get(position).getBody()).getMessage();
            sendMessageViewHolder.content.setText(message);

            //更新时间戳
            if (isShowTimestamp(position)) {
                sendMessageViewHolder.timestamp.setVisibility(View.VISIBLE);
                String timestampString = DateUtils.getTimestampString(new Date(messages.get(position).getMsgTime()));
                sendMessageViewHolder.timestamp.setText(timestampString);
            } else {
                sendMessageViewHolder.timestamp.setVisibility(View.GONE);
            }


            //更新进度条,通过一条message里面的消息状态来修改对应的图片或者动画
            switch (messages.get(position).status()) {
                case INPROGRESS:
                    //消息发送中,展示进度条并且开启动画
                    sendMessageViewHolder.progress.setVisibility(View.VISIBLE);
                    sendMessageViewHolder.progress.setImageResource(R.drawable.progress);
                    AnimationDrawable animationDrawable =
                            (AnimationDrawable) sendMessageViewHolder.progress.getDrawable();
                    animationDrawable.start();
                    break;
                case SUCCESS:
                    //消息发送成功,隐藏进度条
                    sendMessageViewHolder.progress.setVisibility(View.GONE);
                    break;
                case FAIL:
                    //消息发送失败,展示进度条View,并且将进度条的图片替换为红叹号
                    sendMessageViewHolder.progress.setVisibility(View.VISIBLE);
                    sendMessageViewHolder.progress.setImageResource(R.drawable.baseline_error_24);
                    break;
            }
        } else {
            //接收消息绑定布局的部分
            ReceiveMessageViewHolder receiveMessageViewHolder = (ReceiveMessageViewHolder) holder;
            //消息内容的赋值
            String message = ((EMTextMessageBody) messages.get(position).getBody()).getMessage();
            receiveMessageViewHolder.content.setText(message);

            //更新时间戳
            if (isShowTimestamp(position)) {
                receiveMessageViewHolder.timestamp.setVisibility(View.VISIBLE);
                String timestampString = DateUtils.getTimestampString(new Date(messages.get(position).getMsgTime()));
                receiveMessageViewHolder.timestamp.setText(timestampString);
            } else {
                receiveMessageViewHolder.timestamp.setVisibility(View.GONE);
            }
        }
    }

    private boolean isShowTimestamp(int position) {
        //如果是第一条消息,或者与之前一条的消息间隔超过30秒,这两种情况都是true
        boolean showTimestamp = true;
        if (position > 0) {
            showTimestamp = !DateUtils.isCloseEnough(messages.get(position).getMsgTime(),
                    messages.get(position - 1).getMsgTime());
        }
        return showTimestamp;
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    static class SendMessageViewHolder extends RecyclerView.ViewHolder {
        ImageView progress;
        TextView timestamp, content;

        public SendMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            progress = itemView.findViewById(R.id.send_message_progress);
            timestamp = itemView.findViewById(R.id.send_message_timestamp);
            content = itemView.findViewById(R.id.send_message_tv);
        }
    }

    static class ReceiveMessageViewHolder extends RecyclerView.ViewHolder {
        TextView timestamp, content;

        public ReceiveMessageViewHolder(@NonNull View itemView) {
            super(itemView);
            timestamp = itemView.findViewById(R.id.receive_message_timestamp);
            content = itemView.findViewById(R.id.receive_message_tv);
        }
    }
}
